package day317;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author tmh
 * @date 2023/3/17 20:34
 * @description
 */
public class T20 {
    /**
     * deque
     * add():在有空间的情况下将参数中传递的元素插入到deque的末端
     * poll():改方法用于检索和移除deque的头部
     * @param s
     * @return
     */
    public boolean isValid(String s) {
        Deque<Character> deque=new LinkedList<>();
        for(int i = 0;i<s.length();i++){
            char c=s.charAt(i);
            if('('==c){
                deque.push(')');
            }else if('['==c){
                deque.push(']');
            }else if('{'==c){
                deque.push('}');
            }else if(deque.isEmpty()||deque.peek()!=c){
                return false;
            }else{
                deque.pop();
            }
        }
        return deque.isEmpty();
    }
}
